package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/msg/model"
)

type MessageMeMapper struct {
}

/**
 * 保存我的消息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *MessageMeMapper) SaveMessageMe(data *model.MessageMe) bool {
	_, err := orm.NewOrm().Insert(data)
	return nil == err
}

/**
 * 根据ID删除文章评论消息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *MessageMeMapper) DelMessageMeById(id int, userId uint64, systemId uint64) bool {
	// 创建orm对象
	_, err := orm.NewOrm().Raw("delete from xk_msg_me where id = ? and user_id = ? and system_id = ?", id, userId, systemId).Exec()
	return err == nil
}

/**
 * 文章评论已读和未读
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *MessageMeMapper) UpdateMessageMeMarkById(id int, userId uint64, systemId uint64, mark int) bool {
	// 创建orm对象
	_, err := orm.NewOrm().Raw("update xk_msg_me set mark = ? where id = ? and user_id = ?  and system_id = ?", mark, id, userId, systemId).Exec()
	return err == nil
}

/**
 * 一键已读
 * @author feige
 * @date 2024-01-07
 * @version 1.0
 * @desc
 */
func (mapper *MessageMeMapper) UpdateMessageMeMarkByUserId(userId uint64, systemId uint64, mark int) bool {
	// 创建orm对象
	_, err := orm.NewOrm().Raw("update xk_msg_me set mark = ? where  user_id = ? and system_id = ?", mark, userId, systemId).Exec()
	return err == nil
}

/**
 * @desc 根据ID查询消息
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *MessageMeMapper) GetMessageMeById(id int, userId uint64, systemId uint64) (*model.MessageMe, error) {
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 准备一个切片容器
	var messageMe model.MessageMe
	// 执行查询
	err := mysql.Raw("select * from xk_msg_me where  id = ? and user_id = ? and system_id = ?").SetArgs(id, userId, systemId).QueryRow(&messageMe)
	if err != nil {
		return nil, err
	}
	return &messageMe, nil
}

/**
 * @desc 根据ID查询消息
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *MessageMeMapper) CountMessageMeByUserId(mtype int, userId uint64, systemId uint64) bool {
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 准备一个切片容器
	var total int
	// 执行查询
	err := mysql.Raw("select count(1) from xk_msg_me where  mtype = ? and user_id = ? and system_id = ?").SetArgs(mtype, userId, systemId).QueryRow(&total)
	if err != nil {
		return false
	}
	return total == 0
}

/**
 * @desc 根据ID查询消息
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *MessageMeMapper) CountMessageMeByOpid(mtype int, opid uint64, userId uint64, systemId uint64) bool {
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 准备一个切片容器
	var total int
	// 执行查询
	err := mysql.Raw("select count(1) from xk_msg_me where opid = ? and  mtype = ? and user_id = ? and system_id = ?").SetArgs(opid, mtype, userId, systemId).QueryRow(&total)
	if err != nil {
		return false
	}
	return total == 0
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 我的消息
 */
func (mapper *MessageMeMapper) FindMessageMePage(userId uint64, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	messageMeList := []model.MessageMe{}
	//创建orm对象
	mysql := orm.NewOrm()
	cond := orm.NewCondition()
	// 开始执行sql查询
	qs := mysql.QueryTable("xk_msg_me")
	// 查看我的消息
	cond = cond.And("user_id", userId).And("mark", 0).And("system_id", systemId)
	// 执行count查询
	total, err1 := qs.SetCond(cond).Count()
	if err1 != nil {
		return nil, err
	}
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 分页查询
	_, err2 := qs.SetCond(cond).OrderBy("-create_time").Limit(page.PageSize, page.CurrentPage).All(&messageMeList,
		"id",
		"user_id",
		"msg",
		"mark",
		"create_time",
		"mtype",
		"url",
		"uuid",
		"opid",
		"title",
		"cover",
		"description",
		"username",
		"os",
		"ip",
		"browser",
		"avatar",
	)
	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = messageMeList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 系统消息
 */
func (mapper *MessageSystemMapper) FindMessageMePage(userId, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	messageMeList := []model.MessageMe{}
	//创建orm对象
	mysql := orm.NewOrm()
	cond := orm.NewCondition()
	// 开始执行sql查询
	qs := mysql.QueryTable("xk_msg_me")
	// 设定两个条件
	cond = cond.And("system_id", systemId)
	// 设定两个条件
	cond = cond.And("user_id", userId)
	// 执行count查询
	total, err1 := qs.SetCond(cond).Count()
	if err1 != nil {
		return nil, err
	}
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 分页查询
	_, err2 := qs.SetCond(cond).OrderBy("mark", "-create_time").Limit(page.PageSize, page.CurrentPage).All(&messageMeList,
		"id",
		"user_id",
		"msg",
		"mark",
		"mtype",
		"url",
		"uuid",
		"opid",
		"title",
		"cover",
		"description",
		"username",
		"avatar",
		"ip",
		"os",
		"browser",
		"create_time",
		"update_time",
	)
	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = messageMeList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}
